package com.smooth.site.sx.pc.company.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.modules.sys.service.SystemService;
import com.smooth.dao.gen.BizCompanyMapper;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizCompanyExample;
import com.smooth.dao.genx.GxCompMapper;
import com.smooth.dao.genx.bean.HotCompRsp;
import com.smooth.site.sx.mag.biz.comp.service.BizCompanyService;
import com.smooth.site.sx.pc.company.enums.JobFlagEnums;
import com.smooth.site.sx.pc.company.req.CompRegReq;
import com.smooth.site.sx.pc.company.req.HotCompReq;
import com.smooth.sys.site.biz.sms.bx.service.SmsCodeService;

@Component
public class PcCompService {
	@Autowired
	SmsCodeService smsCodeService;
	@Autowired
	BizCompanyService bizCompanyService;
	@Autowired
	BizCompanyMapper bizCompanyMapper;
	public BizCompany registerComp(CompRegReq compRegReq) {
		if(smsCodeService.verfyCode(compRegReq.getTelNo(), compRegReq.getVerCode())) {
			BizCompany bizCompany = new BizCompany();
			bizCompany.setContactTelNo(compRegReq.getTelNo());
			bizCompany.setAccountNo(compRegReq.getAccountNo());
			bizCompany.setCompName(compRegReq.getCompName());
			bizCompany.setJobFlag(JobFlagEnums.nomal.value);
			if(StringUtils.isNotBlank(compRegReq.getPassword())) {
				String enPassword = SystemService.entryptPassword(compRegReq.getPassword());
				
				bizCompany.setPassword(enPassword);
			}
			bizCompanyService.save(bizCompany);
			return bizCompany;
		}else {
			throw new SiteException(SiteCodeEnums.param_error,"短信验证码错误，请重新获取");
		}
	}
	@Autowired
	GxCompMapper gxCompMapper;
	public List<HotCompRsp> queryHotComp(HotCompReq hotCompReq){
		List<HotCompRsp> hotList = gxCompMapper.queryRootCollegeProvinces(hotCompReq.getCityId(),hotCompReq.getQueryStr(),hotCompReq.getLimit(),hotCompReq.getOffset());
		return hotList;
	}
	public BizCompany queryById(String id) {
		return bizCompanyService.get(id);
	}
	public BizCompany queryCompByAccountNo(String accountNo) {
		BizCompanyExample bizCompanyExample = new BizCompanyExample();
		bizCompanyExample.createCriteria().andAccountNoEqualTo(accountNo);
		List<BizCompany>  blist = bizCompanyMapper.selectByExample(bizCompanyExample);
		if(blist.size()>0) {
			return blist.get(0);
		}
		return null;
	}
	
	public BizCompany queryCompByTelNo(String telNo) {
		BizCompanyExample bizCompanyExample = new BizCompanyExample();
		bizCompanyExample.createCriteria().andContactTelNoEqualTo(telNo);
		List<BizCompany>  blist = bizCompanyMapper.selectByExample(bizCompanyExample);
		if(blist.size()>0) {
			return bizCompanyService.get(blist.get(0).getId());
		}
		return null;
	}
	
	public BizCompany updComp(BizCompany bizCompany) {
		bizCompanyMapper.updateByPrimaryKeySelective(bizCompany);
		return bizCompanyMapper.selectByPrimaryKey(bizCompany.getId());
	}
}
